<template>
  <div class="container">
    <topTitle :titleText="titleText" :titleExplain="titleExplain"></topTitle>
    <div class="mainCon">
      <el-tabs
        v-model="activeName"
        @tab-click="handleClick"
        class="commonTab"
        type="card"
      >
        <el-tab-pane label="银行回传配置" name="first"> </el-tab-pane>
        <el-tab-pane label="拨付计划配置" name="second"> </el-tab-pane>
        <el-tab-pane label="工伤保险审核配置" name="third"> </el-tab-pane>
        <el-tab-pane label="问题反馈短信配置" name="four"> </el-tab-pane>
        <el-tab-pane label="项目状态变更审核配置" name="five"> </el-tab-pane>
      </el-tabs>
      <el-form
        class="eltabBox"
        :model="formData"
        :rules="rules"
        ref="ruleForm"
        hide-required-asterisk
        v-if="activeName !== 'four' && activeName !== 'five'"
      >
        <div class="title">
          {{ title }}
          <el-button
            v-show="isShowUpdate"
            :loading="saveLoading"
            class="commonBut saveBtn funComBut fr"
            @click="handleAdd('ruleForm')"
            style="margin-left: 124px"
          >
            保存
          </el-button>
        </div>
        <div v-show="activeName === 'first'">
          <el-form-item
            label="是否允许银行手动回传专户流水"
            class="row-padding-bottom"
          >
            <el-radio-group v-model="bankAcountValue">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>
        <div style="margin-bottom: 38px" v-show="activeName === 'first'">
          <el-form-item
            label="是否允许银行手动回传工资结果"
            class="row-padding-bottom"
          >
            <el-radio-group v-model="bankSalaryValue">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>
        <div style="margin-bottom: 38px" v-show="activeName === 'second'">
          <el-form-item
            label="创建拨付计划方式"
            prop="allocationPlan"
            class="row-padding-bottom"
          >
            <el-select
              filterable
              clearable
              style="width: 340px"
              v-model="formData.allocationPlan"
            >
              <el-option
                v-for="item in allocationPlan"
                :key="item.type"
                :label="item.name"
                :value="item.type"
              ></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div style="margin-bottom: 38px" v-show="activeName === 'third'">
          <el-form-item
            label="工伤保险备案审核"
            class="row-padding-bottom insurance"
          >
            <el-radio-group
              v-model="insuranceValue"
              @change="changeInsuranceValue"
            >
              <el-radio label="1">开启审核</el-radio>
              <el-radio label="0">关闭审核</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            label="历史数据设置"
            class="row-padding-bottom insurance"
            v-show="showStatusValue"
          >
            <el-radio-group v-model="statusValue">
              <el-radio label="AUDIT">设置为待审核</el-radio>
              <el-radio label="APPROVED">设置为审核通过</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>
      </el-form>
      <el-form
        class="el-form1 eltabBox"
        :model="formDataFour"
        ref="formDataFour"
        :rules="ruleForm"
        v-if="activeName == 'four'"
      >
        <div class="title">
          {{ title }}
          <el-button
            v-show="isShowUpdate"
            :loading="saveFourLoading"
            class="commonBut saveBtn funComBut fr"
            @click="handleAddFour()"
            style="margin-left: 124px"
          >
            保存
          </el-button>
        </div>
        <el-form-item
          label="系统接收到问题反馈时通知受理人"
          class="row-padding-bottom insuranceSty"
        >
          <el-radio-group v-model="formDataFour.notificationReceiver">
            <el-radio label="1">发送短信给受理人</el-radio>
            <el-radio label="0">不发送短信</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="受理人姓名"
          class="row-padding-bottom insuranceSty"
          v-if="formDataFour.notificationReceiver == '1'"
        >
          <el-input
            v-model.trim="formDataFour.receiverName"
            class="form-item"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="手机号码"
          prop="receiverTel"
          class="row-padding-bottom insuranceSty"
          v-if="formDataFour.notificationReceiver == '1'"
        >
          <el-input
            v-model.trim="formDataFour.receiverTel"
            class="form-item"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="问题处理完成通知问题反馈人"
          class="row-padding-bottom insuranceSty"
        >
          <el-radio-group v-model="formDataFour.probFeedback">
            <el-radio label="1">发送短信</el-radio>
            <el-radio label="0">不发送短信</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <!-- 项目状态变更审核配置 -->
      <change-audit-config v-if="activeName == 'five'"></change-audit-config>
    </div>
  </div>
</template>

<script>
import changeAuditConfig from './changeAuditConfig'

export default {
  name: 'systemBasicConfig',
  components: {
    changeAuditConfig,
  },
  data() {
    return {
      titleText: '系统基础配置项',
      activeName: 'first',
      titleExplain: '',
      formData: {
        allocationPlan: '',
      },
      rules: {
        allocationPlan: {
          required: true,
          message: '请选择拨付计划',
          trigger: 'change',
        },
      },
      ruleForm: {
        receiverName: {
          required: true,
          message: '请输入姓名',
          trigger: 'blur',
        },
        receiverTel: {
          required: true,
          pattern: /^1[3456789]\d{9}$/,
          message: '请输入正确的手机号码',
          trigger: 'blur',
        },
      },
      allocationPlan: [
        {
          name: '自动生成',
          type: 'AUTO',
        },
        {
          name: '施工方创建直接生效',
          type: 'MANUAL',
        },
        {
          name: '施工方创建需审核生效',
          type: 'MANUAL_NEED_AUDIT',
        },
      ],
      saveLoading: false,
      isShowUpdate: '',
      bankAcountId: '',
      bankAcountValue: '',
      bankSalaryId: '',
      bankSalaryValue: '',
      title: '银行回传配置',
      insuranceId: '',
      insuranceValue: '',
      statusValue: '',
      showStatusValue: false,
      formDataFour: {
        notificationReceiver: '0', //通知受理人
        probFeedback: '0', //问题处理完成通知问题反馈人
        receiverTel: '', //手机号
        receiverName: '', //受理人姓名
      },
      saveFourLoading: false,
      fourId: '',
    }
  },
  created() {
    this.getAllocationPlan()
    this.isShowUpdate =
      this.$store.state.buttonLimitsArr.indexOf('systemBasicConfig:update') > -1
    this.getBankAcountdetail()
    this.getBankSalaryDetail()
  },
  methods: {
    handleClick(val) {
      this.title = val.label
      if (val.paneName == 'third') {
        this.getinsuranceDetail()
      }
      if (val.paneName == 'four') {
        //问题反馈短信配置
        this.getProblemFeedbackDetail()
      }
    },
    // 获取当前拨付计划方式
    getAllocationPlan() {
      this.axios({
        method: 'get',
        url: 'api-acc/cs/disbursesetup/expand/get',
      })
        .then((res) => {
          this.formData.allocationPlan = res.data.result
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '获取当前拨付计划方式失败',
            type: 'error',
          })
        })
    },
    // 保存拨付方式
    handleAdd(formName) {
      if (this.activeName == 'third') {
        if (this.insuranceValue == 1) {
          this.saveInsuranceStatus()
        }
        this.saveInsurance()
        return false
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.updateconfigModel(this.bankAcountId, this.bankAcountValue)
          this.updateconfigModel(this.bankSalaryId, this.bankSalaryValue)
          this.saveLoading = true
          this.axios({
            method: 'post',
            url: 'api-acc/cs/disbursesetup/expand/update',
            data: {
              createType: this.formData.allocationPlan,
            },
          })
            .then((response) => {
              this.saveLoading = false
              if (!response.data.hasError) {
                this.$message({
                  message: '保存成功',
                  type: 'success',
                })
              } else {
                this.$message({
                  message: response.data.errorMsg,
                  type: 'error',
                })
              }
            })
            .catch((error) => {
              this.saveLoading = false
            })
        } else {
          return false
        }
      })
    },
    getBankAcountdetail() {
      this.axios({
        method: 'post',
        url: 'api-m/paramconfig/findbyname',
        data: {
          name: 'bankAcountdetail',
        },
      })
        .then((res) => {
          if (!res.data.hasError) {
            this.bankAcountId = res.data.result.id
            this.bankAcountValue = res.data.result.value
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '数据请求失败，请重试',
            type: 'error',
          })
        })
    },
    getBankSalaryDetail() {
      this.axios({
        method: 'post',
        url: 'api-m/paramconfig/findbyname',
        data: {
          name: 'bankSalaryDetail',
        },
      })
        .then((res) => {
          if (!res.data.hasError) {
            this.bankSalaryId = res.data.result.id
            this.bankSalaryValue = res.data.result.value
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '数据请求失败，请重试',
            type: 'error',
          })
        })
    },
    updateconfigModel(id, value) {
      this.axios({
        method: 'post',
        url: 'api-m/paramconfig/updateconfigModel',
        data: {
          id: id,
          value: value,
        },
      })
        .then((res) => {
          if (!res.data.hasError) {
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '数据请求失败，请重试',
            type: 'error',
          })
        })
    },
    getinsuranceDetail() {
      this.axios({
        method: 'post',
        url: 'api-m/paramconfig/findbyname',
        data: {
          name: 'empInsAuditConfig',
        },
      })
        .then((res) => {
          if (!res.data.hasError) {
            this.insuranceId = res.data.result.id
            this.insuranceValue = res.data.result.value
            if (this.insuranceValue == '1') {
              this.showStatusValue = true
              this.statusValue = 'APPROVED'
            } else {
              this.showStatusValue = false
            }
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '数据请求失败，请重试',
            type: 'error',
          })
        })
    },
    saveInsuranceStatus() {
      this.axios({
        method: 'post',
        url: 'api-e/cs/ins/expand/editall',
        data: {
          status: this.statusValue,
        },
      })
        .then((res) => {
          if (!res.data.hasError) {
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '数据请求失败，请重试',
            type: 'error',
          })
        })
    },
    saveInsurance() {
      this.axios({
        method: 'post',
        url: 'api-m/paramconfig/updateconfigModel',
        data: {
          id: this.insuranceId,
          value: this.insuranceValue,
        },
      })
        .then((res) => {
          if (!res.data.hasError) {
            this.$message({
              message: '保存成功',
              type: 'success',
            })
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '数据请求失败，请重试',
            type: 'error',
          })
        })
    },
    changeInsuranceValue(val) {
      if (val == '1') {
        this.showStatusValue = true
        this.statusValue = 'APPROVED'
      } else {
        this.showStatusValue = false
      }
    },
    //问题反馈短信配置
    getProblemFeedbackDetail() {
      this.axios({
        method: 'post',
        url: 'api-m/paramconfig/findbyname',
        data: {
          name: 'feedbackConfig',
        },
      })
        .then((res) => {
          if (!res.data.hasError) {
            var arr = res.data.result.value.split(',')
            this.formDataFour = {
              notificationReceiver: arr[0], //通知受理人
              probFeedback: arr[3], //问题处理完成通知问题反馈人
              receiverTel: arr[2], //手机号
              receiverName: arr[1], //受理人姓名
            }
            this.fourId = res.data.result.id
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
          }
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '数据请求失败，请重试',
            type: 'error',
          })
        })
    },
    // 保存
    handleAddFour() {
      this.saveFourLoading = true
      this.$refs.formDataFour.validate((valid) => {
        if (valid) {
          this.saveProblemFeedback()
        } else {
          this.saveFourLoading = false
          return
        }
      })
    },
    saveProblemFeedback() {
      var newVal
      if (this.formDataFour.notificationReceiver == '0') {
        this.formDataFour.receiverName = ''
        this.formDataFour.receiverTel = ''
      }
      newVal =
        this.formDataFour.notificationReceiver +
        ',' +
        this.formDataFour.receiverName +
        ',' +
        this.formDataFour.receiverTel +
        ',' +
        this.formDataFour.probFeedback
      this.axios({
        method: 'post',
        url: 'api-m/paramconfig/updateconfigModel',
        data: {
          id: this.fourId,
          value: newVal,
        },
      })
        .then((res) => {
          if (!res.data.hasError) {
            this.$message({
              message: '保存成功',
              type: 'success',
            })
          } else {
            this.$message({
              type: 'error',
              message: response.data.errorMsg,
            })
          }
          this.saveFourLoading = false
        })
        .catch((error) => {
          this.$message({
            showClose: true,
            duration: 3000,
            message: '数据请求失败，请重试',
            type: 'error',
          })
          this.saveFourLoading = false
        })
    },
  },
}
</script>

<style scoped lang="less">
.saveBtn {
  height: 36px;
  font-family: SourceHanSansCN-Normal;
  font-size: 14px;
  color: #ffffff;
  text-align: center;
}

.mainCon {
  height: calc(100% - 67px);
  overflow-y: auto;
  padding-top: 0;
  // padding-left: 24px;
  // padding-right: 24px;
  // padding-top: 10px;
}

.eltabBox {
  padding: 24px;
}

// 标题栏样式
.title {
  margin-top: 10px;
  margin-bottom: 24px;
  font-size: 16px;
  color: #333;
  font-weight: bold;
}

.insurance /deep/ .el-form-item__label {
  width: 125px;
}
.insuranceSty /deep/ .el-form-item__label {
  width: 222px;
}
.insuranceSty /deep/.el-input__inner {
  width: 260px;
}
.form-item {
  width: 300px;
}
.el-form1 /deep/ .el-form-item__error {
  left: 220px !important;
}
</style>
